<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <style>
    img {
      width: 1200px;
      height: auto;
    }
  </style>
</head>

<body>
  <h3></h3>

  <div>
    <h3>使用流的方式，将图片转为灰度</h3>
    <img src="./2.png" />
  </div>

  <div>
    <img id='target' />
  </div>

  <script src="./pako.min.js"></script>
  <script src="./crc32.js"></script>
  <script src="./png-lib.js"></script>

  <script>
    const image = document.getElementById('target');

    const run = async () => {
      try {


        const response = await fetch('./2.png');

        const rs = response.body

        // TransformStream 是一个内置的流转换器，它可以将可读流转换为可写流
        // https://developer.mozilla.org/zh-CN/docs/Web/API/TransformStream
        // GrayscalePNGTransformer 是  stream/01/png-lib.js 里面实现的

        const r2 = await rs.pipeThrough(new TransformStream(new GrayscalePNGTransformer()))


        const newResponse = new Response(r2)


        const blob = await newResponse.blob()

        console.log('blob', blob);

        image.src = URL.createObjectURL(blob)

      } catch (error) {
        console.log('报错了');

        console.log(error)
      }
    }

    run()




  </script>
</body>

</html>